package cn.iocoder.yudao.module.book.dal.mysql.community;

import java.util.*;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.book.dal.dataobject.community.TopicsDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

/**
 * 话题 Mapper
 *
 * @author system
 */
@Mapper
public interface TopicsMapper extends BaseMapperX<TopicsDO> {

    /**
     * 查询热门话题
     */
    default List<TopicsDO> selectHotTopics(Integer limit) {
        return selectList(new LambdaQueryWrapperX<TopicsDO>()
                .eq(TopicsDO::getStatus, 1)
                .eq(TopicsDO::getIsHot, 1)
                .orderByDesc(TopicsDO::getPostsCount)
                .last("LIMIT " + limit));
    }

    /**
     * 搜索话题
     */
    default List<TopicsDO> searchTopics(String keyword) {
        return selectList(new LambdaQueryWrapperX<TopicsDO>()
                .eq(TopicsDO::getStatus, 1)
                .like(TopicsDO::getTopicName, keyword)
                .orderByDesc(TopicsDO::getPostsCount));
    }

    /**
     * 根据话题名称查询
     */
    default TopicsDO selectByTopicName(String topicName) {
        return selectOne(new LambdaQueryWrapperX<TopicsDO>()
                .eq(TopicsDO::getTopicName, topicName)
                .eq(TopicsDO::getStatus, 1));
    }

    /**
     * 更新动态数量
     */
    int updatePostsCount(@Param("topicId") Long topicId, @Param("increment") Integer increment);

    /**
     * 更新关注数量
     */
    int updateFollowersCount(@Param("topicId") Long topicId, @Param("increment") Integer increment);

}
